﻿<view:AViewPage 
    x:Class="OneBusAway.WP7.View.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:pivot="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"  
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
    xmlns:data="clr-namespace:OneBusAway.WP7.ViewModel;assembly=OneBusAway.WP7.ViewModel" 
    xmlns:view="clr-namespace:OneBusAway.WP7.View;assembly=OneBusAway.WP7.View"
    xmlns:local="clr-namespace:OneBusAway.WP7.View"
    xmlns:m="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps" 
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="728"
    xmlns:tilt="clr-namespace:ControlTiltEffect"
    tilt:TiltEffect.IsTiltEnabled="True"
    shell:SystemTray.IsVisible="False"
>

    <view:AViewPage.Resources>
        <data:MainPageVM x:Key="ViewModel" />

        <DataTemplate x:Key="PushpinTemplate">
            <m:Pushpin PositionOrigin="{Binding location}" />
        </DataTemplate>



        <!--<viewModel:RouteDirectionsMV x:Key="RouteStopsList" />-->

        <view:StopRoutesConverter x:Key="StopRoutesConverter"></view:StopRoutesConverter>
        <view:DistanceConverter x:Key="DistanceConverter"></view:DistanceConverter>
        <view:DateTimeDeltaConverter x:Key="DateTimeDeltaConverter"></view:DateTimeDeltaConverter>
        <view:StopDirectionConverter x:Key="StopDirectionConverter"></view:StopDirectionConverter>
        <view:VisibilityConverter x:Key="VisibilityConverter"></view:VisibilityConverter>
        <view:LowercaseConverter x:Key="LowercaseConverter"></view:LowercaseConverter>
        <view:ShortDistanceConverter x:Key="ShortDistanceConverter"></view:ShortDistanceConverter>


        <DataTemplate x:Key="StopTemplate">

            <Grid Margin="5">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />

                    <ColumnDefinition Width="215" />
                    <ColumnDefinition Width="215" />
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                </Grid.RowDefinitions>

                <Polygon CacheMode="BitmapCache" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Grid.RowSpan="3"  Points="0,0 32,0 32,32 0,61" Fill="{StaticResource OBAForegroundBrush}" Opacity="1.0"/>
                <Canvas CacheMode="BitmapCache" Grid.Row="0" Grid.Column="0" Grid.RowSpan="3"  x:Name="BusIcon" Margin="12">
                    <Canvas.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="0.85" ScaleY="0.85" />
                        </TransformGroup>
                    </Canvas.RenderTransform>
                    <Rectangle Width="26" Height="22" Canvas.Left="1" Canvas.Top="2" Fill="{StaticResource OBABackgroundBrush}" Stroke="{StaticResource OBABackgroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Ellipse Width="6" Height="6" Canvas.Left="19" Canvas.Top="21" Stretch="Fill" StrokeLineJoin="Round" Stroke="{StaticResource OBABackgroundBrush}" Fill="{StaticResource OBABackgroundBrush}"/>
                    <Ellipse Width="6" Height="6" Canvas.Left="3" Canvas.Top="21" Stretch="Fill" StrokeLineJoin="Round" Stroke="{StaticResource OBABackgroundBrush}" Fill="{StaticResource OBABackgroundBrush}"/>

                    <Rectangle Width="22" Height="10" Canvas.Left="3" Canvas.Top="7" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Rectangle Width="6" Height="3" Canvas.Left="3" Canvas.Top="19" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Rectangle Width="6" Height="3" Canvas.Left="19" Canvas.Top="19" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Rectangle Width="22" Height="3" Canvas.Left="3" Canvas.Top="3" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>

                    <Path Fill="{StaticResource OBABackgroundBrush}" Stretch="Fill" Stroke="{StaticResource OBABackgroundBrush}" StrokeLineJoin="Round" Height="12" Width="1" UseLayoutRounding="False" Canvas.Left="13.5" Canvas.Top="6.5" Data="M16,8 L16,17"/>
                </Canvas>

                <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Text="{Binding name}" Foreground="{StaticResource OBAForegroundBrush}" />
                <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Foreground="{StaticResource OBASubtleBrush}" FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Converter={StaticResource StopRoutesConverter}}" />
                <TextBlock Grid.Column="1" Grid.Row="2" Foreground="{StaticResource OBASubtleBrush}" FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Converter={StaticResource StopDirectionConverter}}" />
                <TextBlock Grid.Column="2" Grid.Row="2" Foreground="{StaticResource OBASubtleBrush}" FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Converter={StaticResource DistanceConverter}, ConverterParameter={StaticResource ViewModel}}" />
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="RouteTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="90" />
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition Width="200" />
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                </Grid.RowDefinitions>

                <StackPanel Grid.Column="0" VerticalAlignment="Top" Grid.Row="1" Grid.RowSpan="3" Margin="10,0,0,0" Orientation="Vertical">
                    <TextBlock Text="{Binding Path=Route.shortName}" FontSize="{StaticResource PhoneFontSizeExtraLarge}"  Foreground="{StaticResource OBAForegroundBrush}" />
                    <TextBlock Foreground="{StaticResource OBASubtleBrush}"  Margin="2,0,0,0" FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Path=Route, Converter={StaticResource ShortDistanceConverter}, ConverterParameter={StaticResource ViewModel}}" />
                </StackPanel>
                <StackPanel Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="3"  >
                    <ItemsControl ItemsSource="{Binding Path=RouteStops}" Height="105">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <ListBoxItem x:Name="DirectionButton"  Content="{Binding Path=name, Converter={StaticResource LowercaseConverter}}"  
                                        FontSize="{StaticResource PhoneFontSizeLarge}"  
                                        Foreground="{StaticResource OBAForegroundBrush}"
                                         HorizontalContentAlignment="Left"
                                         MouseLeftButtonUp="DirectionButton_MouseLeftButtonUp"
                                         BorderThickness="0" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="FavTemplate">
            <Grid Margin="5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80" />
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition Width="200" />
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                </Grid.RowDefinitions>

                <TextBlock Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" VerticalAlignment="Center" Text="{Binding Path=route.shortName}" FontSize="{StaticResource PhoneFontSizeLarge}" Margin="5"  Foreground="{StaticResource OBAForegroundBrush}" />

                <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Text="{Binding Path=Title}"  Foreground="{StaticResource OBAForegroundBrush}" />

                <TextBlock Grid.Column="1" Grid.Row="1" Foreground="{StaticResource OBASubtleBrush}" FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Path=stop, Converter={StaticResource DistanceConverter}, ConverterParameter={StaticResource ViewModel}}" />

                <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" Text="{Binding  Path=Detail}"  Foreground="{StaticResource OBAForegroundBrush}" />
            </Grid>
        </DataTemplate>

        <Storyboard x:Name="SearchStoryboard">
            <DoubleAnimation Duration="0:0:0.2" To="0" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="SearchPanel" d:IsOptimized="True"/>
            <DoubleAnimation Duration="0:0:0.2" To="1.0" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="SearchPanel" d:IsOptimized="True"/>
        </Storyboard>
                       
    </view:AViewPage.Resources>

    <Grid x:Name="LayoutRoot" Background="{StaticResource OBAPrimaryBrush}">
        
        <pivot:Pivot Name="PC" Foreground="{StaticResource OBAForegroundBrush}" >
            <pivot:PivotItem Header="routes">
                <ListBox 
                    x:Name="RoutesListBox" 
                    ItemsSource="{Binding Path=DisplayRouteForLocation, Source={StaticResource ViewModel}}" 
                    ItemTemplate="{StaticResource RouteTemplate}"
                    />
            </pivot:PivotItem>
            <pivot:PivotItem Header="stops">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <!--<Image Grid.Row="0" Source="/Images/appbar.refresh.rest.png" />-->
                    <Button 
                        Grid.Row="0" 
                        x:Name="stopsMapBtn" 
                        Click="stopsMapBtn_Click" 
                        BorderBrush="Transparent" 
                        BorderThickness="0" 
                        HorizontalContentAlignment="Stretch" 
                        VerticalContentAlignment="Stretch" 
                        Padding="0"
                        >
                        <m:Map x:Name="StopsMap" CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" ScaleVisibility="Collapsed" CredentialsProvider="{StaticResource MapCredentials}"
                               Center="{Binding Path=LocationTracker.CurrentLocationSafe, Source={StaticResource ViewModel}}"  ZoomLevel="17" IsEnabled="False">
                            <m:MapLayer x:Name="BusStopsLayer">
                                <m:MapItemsControl x:Name="StopsMapItemsControl" ItemsSource="{Binding Path=StopsForLocation, Source={StaticResource ViewModel}}">
                                    <m:MapItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <m:Pushpin Location="{Binding location}" Style="{StaticResource BusStopSmallPushpinStyle}" PositionOrigin="Center" />
                                        </DataTemplate>
                                    </m:MapItemsControl.ItemTemplate>
                                </m:MapItemsControl>
                            </m:MapLayer>
                            <m:MapLayer x:Name="MyLocationLayer">
                                <m:MapLayer.Children>
                                    <m:Pushpin Location="{Binding Path=LocationTracker.CurrentLocationSafe, Source={StaticResource ViewModel}}" PositionOrigin="Center" Style="{StaticResource MyLocationPushpinStyle}" />
                                </m:MapLayer.Children>
                            </m:MapLayer>
                        </m:Map>
                    </Button>
                    <ListBox Grid.Row="1"
                    x:Name="StopsListBox" 
                    ItemsSource="{Binding Path=StopsForLocation, Source={StaticResource ViewModel}}" 
                    ItemTemplate="{StaticResource StopTemplate}" 
                    SelectionChanged="StopsListBox_SelectionChanged"
                    />
                </Grid>
                
            </pivot:PivotItem>
            <pivot:PivotItem Header="recent">
                <ListBox 
                    x:Name="RecentsListBox" 
                    ItemsSource="{Binding Path=Recents, Source={StaticResource ViewModel}}" 
                    ItemTemplate="{StaticResource FavTemplate}" 
                    SelectionChanged="RecentsListBox_SelectionChanged" 
                    />
            </pivot:PivotItem>
            <pivot:PivotItem Header="favorites">
                <ListBox 
                    x:Name="FavoritesListBox" 
                    ItemsSource="{Binding Path=Favorites, Source={StaticResource ViewModel}}" 
                    ItemTemplate="{StaticResource FavTemplate}" 
                    SelectionChanged="FavoritesListBox_SelectionChanged" 
                    />
            </pivot:PivotItem>
        </pivot:Pivot>
        <StackPanel x:Name="SearchPanel" Opacity="0" Background="{StaticResource OBADarkBrush}" Height="80" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5">
            <StackPanel.RenderTransform>
                <CompositeTransform TranslateY="-75"/>
            </StackPanel.RenderTransform>
            <TextBox x:Name="SearchInputBox" LostFocus="SearchInputBox_LostFocus" KeyUp="SearchInputBox_KeyUp" InputScope="Number" />
            <TextBlock 
				Text="By route number, stop number, location, or address"  
				Foreground="{StaticResource OBASubtleBrush}" FontSize="16" HorizontalAlignment="Center">
				<TextBlock.RenderTransform>
				<CompositeTransform TranslateY="-14"/>
			</TextBlock.RenderTransform>
            </TextBlock>
        </StackPanel>

        <Rectangle
            Height="32"
            Fill="{StaticResource OBADarkBrush}"
            VerticalAlignment="Top"
            Visibility="{Binding Path=Loading, Source={StaticResource ViewModel}, Converter={StaticResource VisibilityConverter}}"
            />

        <TextBlock 
            Visibility="{Binding Path=Loading, Source={StaticResource ViewModel}, Converter={StaticResource VisibilityConverter}}"
            Text="{Binding Path=LoadingText, Source={StaticResource ViewModel}}"
            Foreground="{StaticResource OBAForegroundBrush}"
            VerticalAlignment="Top"
            FontSize="20"
            Margin="23,0,0,0"
            />
        
        <ProgressBar 
            Visibility="{Binding Path=Loading, Source={StaticResource ViewModel}, Converter={StaticResource VisibilityConverter}}"
            IsIndeterminate="True" 
            Height="4" 
            HorizontalAlignment="Left" 
            x:Name="LoadingProgressBar" 
            VerticalAlignment="Top" 
            Width="480" 
            Style="{StaticResource PerformanceProgressBar}"
            Foreground="{StaticResource OBALightBrush}" 
            />
    </Grid>

    <view:AViewPage.ApplicationBar>
        <shell:ApplicationBar ForegroundColor="{StaticResource OBAForegroundColor}" IsVisible="True" IsMenuEnabled="True" BackgroundColor="{StaticResource OBADarkColor}">
            <shell:ApplicationBarIconButton x:Name="appbar_center" IconUri="/Images/appbar.refresh.rest.png" Text="Refresh" Click="appbar_refresh_Click"></shell:ApplicationBarIconButton>
            <shell:ApplicationBarIconButton x:Name="appbar_search" IconUri="/Images/appbar.feature.search.rest.png" Text="Search" Click="appbar_search_Click"></shell:ApplicationBarIconButton>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem x:Name="appbar_settings" Click="appbar_settings_Click" Text="settings"></shell:ApplicationBarMenuItem>
                <shell:ApplicationBarMenuItem x:Name="appbar_about" Click="appbar_about_Click" Text="about"></shell:ApplicationBarMenuItem>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </view:AViewPage.ApplicationBar>
</view:AViewPage>
